#!/bin/sh
. ./conf/env.sh

readonly SCRIPT_DIR=$(cd $(dirname $0) && pwd)
readonly SCRIPT_FILENAME=$(basename $0)
readonly MASHREPO=/build/repo/mash

# lock file creation and removal
LOCKFILE=$SCRIPT_DIR/`basename $0`.lock
clean_up()
{
    rm -rf $LOCKFILE
    exit 0
}

clean_up_err()
{
    rm -rf $LOCKFILE
    exit 1 #Exit code 1
}

[ -f $LOCKFILE ] && { echo $LOCKFILE exists; exit 0; }
trap clean_up_err 2 9 15
trap clean_up QUIT EXIT
touch $LOCKFILE

mash_from_id() {
    local id=$1
    echo "${GREEN}mash $id${RESET}"

    mash -f $SCRIPT_DIR/conf/comps-ns7.xml \
        -c $SCRIPT_DIR/mash/mash.conf \
        -o $MASHREPO $id
}

xcreate_repo() {
    local d=$1

    if [ -d $MASHREPO/$d ]; then
        echo "${GREEN}create repo $d${RESET}"
        /usr/bin/createrepo -q -d -o $MASHREPO/$d \
            -g $SCRIPT_DIR/conf/comps-ns7-build.xml \
            --update --skip-stat $MASHREPO/$d &> /dev/null ||
            /usr/bin/createrepo -q -d -o $MASHREPO/$d \
            -g $SCRIPT_DIR/conf/comps-ns7-build.xml \
            --update --skip-stat $MASHREPO/$d
    fi
}

mash_from_id ns7.0-jk
xcreate_repo ns7.0-jk

mash_from_id ns7.0-installer
xcreate_repo ns7.0-installer

exit 0
